package com.persistent.service.impl;

import com.persistent.entity.Test;
import com.persistent.entity.Testb;
import com.persistent.mapper.TestMapper;
import com.persistent.mapper.TestbMapper;
import com.persistent.service.ITestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static org.springframework.transaction.annotation.Isolation.REPEATABLE_READ;
import static org.springframework.transaction.annotation.Isolation.SERIALIZABLE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2020-01-03
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements ITestService {

    @Autowired
    private TestbMapper testbMapper;

    @Override
//    @Transactional(propagation = Propagation.REQUIRED, isolation = SERIALIZABLE)
    public synchronized void add(){
        com.persistent.entity.Test t =
                baseMapper.selectByName("a");
        if(t != null){
            t.setName("b");
            baseMapper.updateById(t);
        }else{
            com.persistent.entity.Test t1 = new com.persistent.entity.Test();
            t1.setName("a");
            baseMapper.insert(t1);
        }
    }

    @Override
    @Transactional
    public void addDubbo() {
        testTran();
    }

    private void testTran(){
        com.persistent.entity.Test t =
                new com.persistent.entity.Test();
        t.setName("a");
        baseMapper.insert(t);

        Testb testb = new Testb();
        testb.setId(1L);
        testbMapper.insert(testb);

        Testb testc = new Testb();
        testc.setId(1L);
        testbMapper.insert(testc);
    }
}
